﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KTool2.Database;

namespace KTool2.Exceptions
{
    class DBException : Exception
    {
        private int errorCode;
        private string description;

        public string Description
        {
            get { return description; }
        }

        public int ErrorCode
        {
            get { return errorCode; }
        }

        public DBException(string message) : base(message) { }

        public DBException(int errorCode, object infoObj)
        {
            this.errorCode = errorCode;
            StringBuilder descriptionBuilder = new StringBuilder();

            switch (errorCode)
            { 
                case 0:
                    descriptionBuilder.Append("'").Append(DBManagement.DataSource).Append("' is missing.\n")
                        //.Append(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).Append(".\n")
                        .Append("Make sure database is available before continue.");
                    break;
                case 1: // Cannot process database at the present, program can retry later on
                    descriptionBuilder.Append("'").Append(DBManagement.DataSource)
                        .Append(" is unaccessable. Maybe it's opened by another process or user. Make them closed before continue.");
                    break;
                case 2: // Cannot find sheet name in database, program should re-identify sheet name
                    descriptionBuilder.Append("'").Append(infoObj.ToString())
                        .Append("' sheet is not found in '").Append(DBManagement.DataSource)
                        .Append("'. Maybe it's removed or renamed by user.");
                    break;
                default:
                    descriptionBuilder.Append("Database: Unexpected error!");
                    if (infoObj != null)
                    {
                        descriptionBuilder.Append("\n").Append(infoObj.ToString());
                    }
                    break;
            }

            this.description = descriptionBuilder.ToString();
        }
    }
}
